package com.xxkj.drugtraceability.net
import android.content.Context
import com.xxkj.drugtraceability.BuildConfig
import com.xxkj.drugtraceability.App
import com.xxkj.drugtraceability.ext.loge
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit

abstract class BaseRetrofitClient() {
    companion object {
        private const val TIME_OUT = 20
        private const val READ_TIME_OUT = 20
        private const val WRITE_TIME_OUT = 20
        lateinit var retrofit: Retrofit
    }

    private val client: OkHttpClient
        get() {
            val builder = OkHttpClient.Builder().cache(null)
            val logging = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
                override fun log(message: String) {
                    message.loge(tag = "requestInfo refactor OkHttp====Message")
                }
            })
            if (BuildConfig.DEBUG) {
                logging.level = HttpLoggingInterceptor.Level.BODY
            } else {
                logging.level = HttpLoggingInterceptor.Level.BASIC
            }
            builder.addInterceptor(logging)
                    //原来是通过拦截器去修改url，来改变baseurl，但是每次都药拦截，
                // 于是现在改用在修改baseurl的时候，通过反射将baseurl修改了，以后就是正确的了，不需要再拦截了
//                    .addInterceptor(BaseUrlInterceptor())
                .addInterceptor(object: Interceptor {
                    override fun intercept(chain: Interceptor.Chain): Response {
                       val original = chain.request()
                        val request:Request
                        request = if (App.token.isNotEmpty()){
                            original.newBuilder()
                                .header("Authorization","Bearer ${App.token}")
                                .header("Content-Type", "application/json")
                                .method(original.method,original.body).build()
                        }else{
                            original.newBuilder()
                                .header("Content-Type", "application/json")
                                .method(original.method,original.body).build()
                        }
                        return chain.proceed(request)
                    }
                })
                    .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
                    .readTimeout(READ_TIME_OUT.toLong(), TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIME_OUT.toLong(), TimeUnit.SECONDS)
//                    .retryOnConnectionFailure(true)

            handleBuilder(builder)

            return builder.build()
        }

    protected abstract fun handleBuilder(builder: OkHttpClient.Builder)

    fun <S> getService(serviceClass: Class<S>, baseUrl: String): S {
         retrofit = Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterDataFactory.create())
            .baseUrl(baseUrl)
            .build()
        return retrofit.create(serviceClass)
    }
}